package mydemo.springcloudalibaba.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import mydemo.springcloudalibaba.api.common.entity.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPost;
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/get/{id}")
    public CommonResult getPaymentById_success(@PathVariable("id") Long id) {
        return new CommonResult(200, "成功" + serverPost);
    }


    @RequestMapping("/hotKey")
    @SentinelResource(value = "hotKey_id", blockHandler = "testFallback")
    public CommonResult hotKey(@RequestParam(value = "a", required = false) String a,
                               @RequestParam(value = "b", required = false) String b) {
        log.info("hotKey" + serverPost);
        return new CommonResult(200, "成功" + serverPost, a + "," + b);
    }
    public CommonResult testFallback(String a, String b, BlockException e){
        e.printStackTrace();
        return new CommonResult(444, "testFallback" + serverPost, a + "," + b);
    }
}
